package com.byedu.util;

import com.byedu.model.exception.ApiException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * Created by jason on 2017/6/4.
 */
public class ZipUtil {

    private static Logger logger = LoggerFactory.getLogger(ZipUtil.class);
    /**
     * 功能：解压 zip 文件，只能解压 zip 文件
     *
     * @param file
     * @param destDir
     * @param copyDirdirPath 备份上传的文件的路径
     */

    public static List<String> unzip(File file, String destDir,String copyDirdirPath, String charset) {

        List<String> listStr = new ArrayList<String>();
       try{
        destDir = destDir.endsWith("\\") ? destDir : destDir + "\\";
        copyDirdirPath = copyDirdirPath.endsWith("\\") ? copyDirdirPath : copyDirdirPath + "\\";
        byte b[] = new byte[1024];
        int length;
        ZipFile zipFile = null;
        ZipEntry zipEntry = null;
        OutputStream outputStream = null;
//        OutputStream copyoutputStream = null;
        InputStream inputStream = null;
           DataOutputStream copyout = null;
        try {
            zipFile = new ZipFile(file, Charset.forName(charset));
            Enumeration enumeration = zipFile.entries();

            while (enumeration.hasMoreElements()) {
                zipEntry = (ZipEntry) enumeration.nextElement();
                File loadFile = new File(destDir + zipEntry.getName());
                File copyloadFile = new File(copyDirdirPath + zipEntry.getName());

                if (zipEntry.isDirectory()&&!loadFile.exists()) {
                    loadFile.mkdirs();
                } else {
                    if (!loadFile.getParentFile().exists()) {
                        loadFile.getParentFile().mkdirs();
                    }
                    if (!copyloadFile.getParentFile().exists()) {
                        copyloadFile.getParentFile().mkdirs();
                    }
                    if (!copyloadFile.exists()) {
                        copyloadFile.createNewFile();
                    }
                     outputStream = new FileOutputStream(loadFile);
//                     copyoutputStream = new FileOutputStream(copyloadFile);
                     copyout = new DataOutputStream(new FileOutputStream(copyloadFile));
                     inputStream = zipFile.getInputStream(zipEntry);

                    while ((length = inputStream.read(b)) > 0) {
                        outputStream.write(b, 0, length);
                        copyout.write(b, 0, length);
                    }
                    copyout.flush();
                    outputStream.flush();
                    listStr.add(destDir + zipEntry.getName());
                }
            }
            return listStr;
        } catch (Exception e) {

            throw new ApiException("解压文件出错");
        }finally {
            if (inputStream != null) {
                inputStream.close();
            }
            if (outputStream != null) {
                outputStream.close();
            }
            if (copyout != null) {
                copyout.close();
            }
        }
    } catch (IOException e) {
        throw new ApiException("文件保存错误");
    }
    }


    /**
     * 保存excel文件
     *
     * @param file
     * @param dirPath
     */

    public static String saveExcel(int filesize, File file,String fileName, String dirPath, String copydirPath) {
        dirPath = dirPath.endsWith("\\") ? dirPath : dirPath + "\\";
        copydirPath = copydirPath.endsWith("\\") ? copydirPath : copydirPath + "\\";
        byte b[] = new byte[filesize];
        String loadFilepath = dirPath + fileName;
        String copyloadFilepath = copydirPath + fileName;
        try {




                    File fp = new File(loadFilepath);
                    File copyfp = new File(copyloadFilepath);
                    File dirPathfile = new File(dirPath);
                    File copydirPathfile = new File(copydirPath);
                    if(!dirPathfile.exists()){
                        dirPathfile.mkdirs();
                    }
                    if(!copydirPathfile.exists()){
                        copydirPathfile.mkdirs();
                    }
                    if (!fp.exists()) {
                        fp.createNewFile();
                    }
                    if (!copyfp.exists()) {
                        copyfp.createNewFile();
                    }



                    DataOutputStream out = new DataOutputStream(new FileOutputStream(fp));
                      DataOutputStream copyout = new DataOutputStream(new FileOutputStream(copyfp));

                    InputStream is = null;
                    try {
                        is = new FileInputStream(file);
                        is.read(b);
                        out.write(b);
                        copyout.write(b);
                        out.flush();
                        copyout.flush();
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    } finally {
                        if (is != null) {
                            is.close();
                        }
                        if (out != null) {
                            out.close();
                        }
                        if (copyout != null) {
                            copyout.close();
                        }
                    }
            return loadFilepath;
        } catch (IOException e) {
            throw new ApiException("文件保存错误");
        }
    }
}
